package top.hekun.study.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.List;
import java.util.Set;

public class SeleniumDemo {
    private static String openUrlScript = new StringBuffer()
            .append(" function openUrl(url) {")
            .append(" window.open(url);")
            .append(" }")
            .append(" openUrl(arguments[0])")
            .toString();

    private static String logUrl = "https://train.casicloud.com/oauth/#login/cmVzcG9uc2VfdHlwZT10b2tlbiZjbGllbnRfaWQ9OTk5JnJlZGlyZWN0X3VyaT1odHRwcyUzQSUyRiUyRnRyYWluLmNhc2ljbG91ZC5jb20mc3RhdGU9JTJGaG9tZSZsYW5nPWNuJmNhbmNlbFJlbWVtYmVyU3RhdGU9MA==";


    static {

        // 驱动位置
        System.setProperty("webdriver.chrome.driver", "C:\\Chrome\\11906045105\\chromedriver.exe");
    }

    public static void main(String[] args) {


        try {
            SeleniumConfig seleniumConfig = new SeleniumConfig();
            ChromeOptions options = new ChromeOptions();
            // 允许所有请求
            options.addArguments("--remote-allow-origins=*");
            // 谷歌驱动
            ChromeDriver webDriver = new ChromeDriver(options);
            // 启动需要打开的网页
            webDriver.get(logUrl);
            Thread.sleep(2000);
            webDriver.findElement(By.id("D38username")).sendKeys("13995171821");

            webDriver.findElement(By.id("D38pword")).sendKeys("as54188AS");
            // 输入内容进行点击
            webDriver.findElement(By.cssSelector("#D38login")).click();
            Thread.sleep(2000);
            // 打开课程页面
            webDriver.executeScript(openUrlScript, "https://train.casicloud.com/#/train-new/class-detail/0da544f8-21d4-4f0a-969c-636aa50d2744");
            Thread.sleep(10_000);

            Set<String> windowHandles = webDriver.getWindowHandles();
            String[] handlesArray = windowHandles.toArray(new String[windowHandles.size()]);
            webDriver.switchTo().window(handlesArray[1]);

            List<WebElement> elementList = webDriver.findElementsByXPath("/html/body/div[1]/div[1]/div[3]/div/div[2]/div/div[2]/div[1]/div/div[2]/div/div[2]/div/div/ul/li");

            for (int i = 0; i < elementList.size(); i++) {
                webDriver.switchTo().window(webDriver.getWindowHandles().toArray(new String[3])[1]);

                WebElement element = elementList.get(i);
                // /html/body/div[1]/div[1]/div[3]/div/div[2]/div/div[2]/div[1]/div/div[2]/div/div[2]/div/div/ul/li/div/div/div[2]
                WebElement state = element.findElement(By.className("ms-train-state"));

                if ("未完成".equals(state.getText().trim())) {
                    // 点击进入新页面
                    element.findElements(By.xpath("div/div/div[1]/div[1]")).get(0).click();
                    Thread.sleep(2000);
                    webDriver.switchTo().window(webDriver.getWindowHandles().toArray(new String[3])[2]);
                    while (true) {
                        Thread.sleep(1000 * 30);
                        WebElement elementByXPath = webDriver.findElementByXPath(
                                "/html/body/div[1]/div[1]/div[3]/div/div[1]/div/div/div[1]/div/div/div/div/div/div[2]/div/div/ul/li/ul/li/div/dl/div[2]/div[2]/div[2]/span"
                        );
                        if ("重新学习".equals(elementByXPath.getText().trim())) {
                            break;
                        }
                    }
                    webDriver.close();
                    Thread.sleep(2000);
                }
            }

            System.out.println("***");
            webDriver.quit();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
//            webDriver.close();
        }


    }

}
